// console.log('index login的js文件');
import userAxios from '../../api/request';
import {saveToken} from "../../utils/storageUtils";
let usernameInp = document.getElementsByName('username')[0];
let pwdInp = document.getElementsByName('pwd')[0];
let loginForm = document.getElementsByName('loginForm')[0];
loginForm.onsubmit = function(e){
    e.preventDefault();
    // 提交数据
    let username = usernameInp.value;
    let pwd = pwdInp.value;
    userAxios({
        method:"post",
        url:'/admin/login',
        //#region 
        // headers:{
        //     'content-type':"application/x-www-form-urlencoded"
        // },
        // data:`username=${username}&pwd=${pwd}`
        data:{
            username,
            pwd 
        }
    }).then(val=>{
        console.log(val);
        if(val.code === 2000){
            // 存储token值到localstorage
            saveToken(val.data.token);

            window.location.href = '/users.html';
        }else{
            alert('抱歉，您输入的账户密码有误，请重新输入')
            usernameInp.value = '';
            pwdInp.value = '';
            usernameInp.focus();
        }
    })
}